package com.wangdong.mijiqiqi.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * 
 * @author zhou
 * 
 */
@SuppressLint("SimpleDateFormat")
public class DateUtil {
	private static String TAG = DateUtil.class.getSimpleName();
	public static final String Y_M = "yyyy 年 MM 月";
	public static final String Y_M_D = "yyyy-MM-dd";
	public static final String M_D = "MM-dd";
	public static final String M_Ds = "MM月dd日";
	public static final String Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";
	public static final String Y_M_D_H_M = "yyyy-MM-dd HH:mm";
	public static final String Y_M_D_H = "yyyy-MM-dd HH";
	public static final String M_D_H_M_S = "MM-dd HH:mm:ss";
	public static final String H_M_S = "HH:mm:ss";
	public static final String H_M = "HH:mm";
	public static final String Y_M_Ds = "yyyy年MM月dd日";
	public static final String H_Ms = "HH时mm分";
	public static final String Y_M_D_H_Ms = "yyyy年MM月dd日HH时mm分";
	public static final String YMDHMMS = "yyyyMMddHHmmss";
	public static final String Y_M_D_H_M_Ss = "yyyy年MM月dd日HH时mm分ss秒";
	public static final String Y_M_D_H_MS = "yyyy年MM月dd日  HH:mm";

	/**
	 * 一分钟的秒值，用于判断上次的更新时间
	 */
	public static final long ONE_MINUTE = 60 * 1000;

	/**
	 * 一小时的秒值，用于判断上次的更新时间
	 */
	public static final long ONE_HOUR = 60 * ONE_MINUTE;

	/**
	 * 一天的秒值，用于判断上次的更新时间
	 */
	public static final long ONE_DAY = 24 * ONE_HOUR;
	/**
	 * 一月的秒值，用于判断上次的更新时间
	 */
	public static final long ONE_MONTH = 30 * ONE_DAY;

	/**
	 * 一年的秒值，用于判断上次的更新时间
	 */
	public static final long ONE_YEAR = 12 * ONE_MONTH;

	/**
	 * 根据给定时间来获得距离现在时间间隔的描述
	 */
	public static String getTheDaytimeInterval(long time) {
		long currentTime = System.currentTimeMillis();
		long timePassed = currentTime - time;
		long timeIntoFormat;
		Log.d(TAG, "timeIntoFormat=    " + time);
		if (timePassed <= ONE_MINUTE) {
			return "刚刚";
		} else if (timePassed < ONE_HOUR) {
			timeIntoFormat = timePassed / ONE_MINUTE;
			return timeIntoFormat + "分钟前";
		} else if (timePassed < ONE_DAY) {
			timeIntoFormat = timePassed / ONE_HOUR;
			return timeIntoFormat + "小时前";
		} else if (timePassed < ONE_MONTH) {
			timeIntoFormat = timePassed / ONE_DAY;
			return timeIntoFormat + "天前";
		} else if (timePassed < ONE_YEAR) {
			timeIntoFormat = timePassed / ONE_MONTH;
			return timeIntoFormat + "个月前";
		} else {
			timeIntoFormat = timePassed / ONE_YEAR;
			return timeIntoFormat + "年前";
			// return null;
		}
	}

	/**
	 * 
	 * @param date
	 *            string类型的日期 例如:1970-01-01 23:23:23
	 * @param format
	 *            传入类型的格式 例如:yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String DateToString(Date date, String format) {
		if (date == null) {
			return null;
		} else if (TextUtils.isEmpty(format)) {
			return new SimpleDateFormat(Y_M_D_H_M_S).format(date);
		} else {
			return new SimpleDateFormat(format).format(date);
		}
	}

	/**
	 * 
	 * @param date
	 *            string类型的日期 例如:1970-01-01 23:23:23
	 * @param format
	 *            传入类型的格式 例如:yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Date StringToDate(String date, String format) {
		try {
			if (TextUtils.isEmpty(date)) {
				return null;
			} else if (TextUtils.isEmpty(format)) {
				Log.d(TAG, "选择时间=  " + date);
				return new SimpleDateFormat(Y_M_D_H_M_S).parse(date);
			} else {

				return new SimpleDateFormat(format).parse(date);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Get one date type in a fixed format.
	 * 
	 * @param pattern
	 * @return
	 */
	public static DateFormat getCnDateFormat(String pattern) {
		return new SimpleDateFormat(pattern);
	}

	/**
	 * 今天，过去，未来
	 * 
	 * @return 1-过去 2-今天 3—未来
	 */
	public static int isToday(String mills) {
		// 当前时间
		Calendar curCalendar = Calendar.getInstance();

		curCalendar.add(Calendar.DATE, 0);

		int year = curCalendar.get(Calendar.YEAR);
		int month = curCalendar.get(Calendar.MONTH);
		int day = curCalendar.get(Calendar.DAY_OF_MONTH);

		// 把时间移动到当天的00:00
		curCalendar.set(year, month, day, 0, 0, 0);

		// 得到00:00的毫秒值
		long curMills = curCalendar.getTimeInMillis();

		// 比较二个毫秒值
		long time = Long.parseLong(mills);

		// 如果传进来的时间，比当天的00:00的毫秒值要小
		if (time < curMills) {
			return 1;
		} else if (curMills <= time && time < (curMills + 24 * 3600 * 1000)) {
			return 2;
		} else if (time >= (curMills + 24 * 3600 * 1000)) {
			return 3;
		}
		return 0;
	}

	/**
	 * 拿到今天偏移后的某天的00:00的毫秒值
	 * 
	 * @param value
	 *            0代表当前 -1往前一天
	 * @return
	 */
	public static long getTimeInMillisForStart(int value) {

		// 当前时间
		Calendar curCalendar = Calendar.getInstance();

		// 移动时间
		curCalendar.add(Calendar.DATE, value);

		int year = curCalendar.get(Calendar.YEAR);
		int month = curCalendar.get(Calendar.MONTH);
		int day = curCalendar.get(Calendar.DAY_OF_MONTH);

		// 把时间移动到当天的00:00
		curCalendar.set(year, month, day, 0, 0, 0);

		// 得到00:00的毫秒值
		long curMills = curCalendar.getTimeInMillis();

		return curMills;
	}

	/**
	 * 拿到今天偏移后的某天的23:59的毫秒值
	 * 
	 * @param value
	 *            0代表当前 -1往前一天
	 * @return
	 */
	public static long getTimeInMillisForEnd(int value) {

		// 当前时间
		Calendar curCalendar = Calendar.getInstance();

		// 移动时间
		curCalendar.add(Calendar.DATE, value);

		int year = curCalendar.get(Calendar.YEAR);
		int month = curCalendar.get(Calendar.MONTH);
		int day = curCalendar.get(Calendar.DAY_OF_MONTH);

		// 把时间移动到当天的00:00
		curCalendar.set(year, month, day, 23, 59, 59);

		// 得到00:00的毫秒值
		long curMills = curCalendar.getTimeInMillis();

		return curMills;
	}

	public static String getTimeFomat(String year, String month, String day,
                                      String hour, String minute, String format) {
		Calendar curCalendar = Calendar.getInstance();
		int year_ = 0;
		if (TextUtils.isEmpty(year)) {
			year_ = curCalendar.get(Calendar.YEAR);
		} else {
			year_ = Integer.parseInt(year);
		}
		int month_ = 0;
		if (TextUtils.isEmpty(month)) {
			month_ = curCalendar.get(Calendar.MONTH);
		} else {
			month_ = Integer.parseInt(month);
		}
		int day_ = 0;
		if (TextUtils.isEmpty(day)) {
			day_ = curCalendar.get(Calendar.DAY_OF_MONTH);
		} else {
			day_ = Integer.parseInt(day);
		}
		int hour_ = 0;
		if ("0".equals(hour)) {
			hour_ = 0;
		} else {
			hour_ = Integer.parseInt(hour);
		}
		int minute_ = 0;
		if ("0".equals(minute)) {
			minute_ = 0;
		} else {
			minute_ = Integer.parseInt(minute);
		}
		int second = curCalendar.get(Calendar.SECOND);
		curCalendar.set(year_, month_, day_, hour_, minute_, second);

		return DateToString(new Date(curCalendar.getTimeInMillis()), format);

	}

	/**
	 * 获取 当前星期 的 00：00的毫秒值
	 * 
	 * @param week
	 * @return
	 */
	public static long getTimeInMillisForWeekStart(String week) {

		int pWeek = Integer.parseInt(week);
		int pToDay = dayForWeek();
		int pTemp = pWeek - pToDay;

		long time = getTimeInMillisForStart(pTemp);

		return time;
	}

	/**
	 * 获取 当前星期 的 23：59:59的毫秒值
	 * 
	 * @param week
	 * @return
	 */
	public static long getTimeInMillisForWeekEnd(String week) {

		int pWeek = Integer.parseInt(week);
		int pToDay = dayForWeek();
		int pTemp = pWeek - pToDay;

		long time = getTimeInMillisForEnd(pTemp);

		return time;
	}

	/**
	 * 返回今天是星期
	 * 
	 * @return
	 * @throws Exception
	 */
	public static int dayForWeek() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	}

	/**
	 * 拿到某天的00:00的毫秒值
	 * 
	 * @param date
	 * 
	 * @return
	 */
	public static long getTimeInMillisForStart(Date date) {

		// 当前时间
		Calendar curCalendar = Calendar.getInstance();

		// 移动时间
		curCalendar.setTime(date);

		int year = curCalendar.get(Calendar.YEAR);
		int month = curCalendar.get(Calendar.MONTH);
		int day = curCalendar.get(Calendar.DAY_OF_MONTH);

		// 把时间移动到当天的00:00
		curCalendar.set(year, month, day, 0, 0, 0);

		// 得到00:00的毫秒值
		long curMills = curCalendar.getTimeInMillis();

		return curMills;
	}

	/**
	 * 拿到某天的23:59的毫秒值
	 * 
	 * @param date
	 * 
	 * @return
	 */
	public static long getTimeInMillisForEnd(Date date) {

		// 当前时间
		Calendar curCalendar = Calendar.getInstance();

		// 移动时间
		curCalendar.setTime(date);

		int year = curCalendar.get(Calendar.YEAR);
		int month = curCalendar.get(Calendar.MONTH);
		int day = curCalendar.get(Calendar.DAY_OF_MONTH);

		// 把时间移动到当天的00:00
		curCalendar.set(year, month, day, 23, 59, 59);

		// 得到00:00的毫秒值
		long curMills = curCalendar.getTimeInMillis();

		return curMills;
	}

	/**
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
	 * 
	 */
	public static String getStringDate(Long date) {
		SimpleDateFormat formatter = new SimpleDateFormat(Y_M_D_H_M_S);
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH
	 * 
	 */
	public static String getStringDateYMD(Long date) {
		SimpleDateFormat formatter = new SimpleDateFormat(Y_M_D_H);
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 将长时间格式字符串转换为时间 MM月dd日
	 * 
	 */
	public static String getStringDateMDS(Long date) {
		SimpleDateFormat formatter = new SimpleDateFormat(M_Ds);
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 判断选择的日期是不是过去时间
	 */
	public static boolean dateIsPast(String date) {
		Log.d(TAG, "选择时间=  " + date);
		long timeNow = System.currentTimeMillis();

		Date timeSelect = StringToDate(date + ":00:00", null);
		Log.d(TAG, "选择时间=  " + timeSelect.getTime() + "系统时间=   " + timeNow);
		if (timeSelect.getTime() <= timeNow) {
			return true;
		} else {
			return false;
		}
		//
		// // 当前时间
		// Calendar curCalendar = Calendar.getInstance();
		// int year = curCalendar.get(Calendar.YEAR);
		// int month = curCalendar.get(Calendar.MONTH) + 1;
		// int day = curCalendar.get(Calendar.DAY_OF_MONTH);
		// String month2 = (month < 10) ? "0" + month : "" + month;
		// String day2 = (day < 10) ? "0" + day : "" + day;
		// Log.d(TAG, "时间=   " + year + "-" + month + "-" + day);
		// if ((year + "-" + month2 + "-" + day2).equals(date)) {
		// return true;
		// } else {
		// return false;
		// }

	}
}
